package com.bq.gametrade.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bq.gametrade.R;
import com.bq.gametrade.bean.RechargeMoneyDetails;

import java.util.ArrayList;

/**
 * Created by WangChang on 2016/4/1.
 */
public class RechargeMoneyListAdapter extends RecyclerView.Adapter<RechargeMoneyListAdapter.BaseViewHolder> {

    private ArrayList<RechargeMoneyDetails> dataList;
    private int lastPressIndex = -1;
    private Context context;



    public RechargeMoneyListAdapter(Context context, ArrayList<RechargeMoneyDetails> list) {
        this.context = context;
        this.dataList = list;
    }

    public void setData(ArrayList<RechargeMoneyDetails> list) {
        this.dataList = list;
    }

    /***
     * 回调
     */
    public interface OnItemClickLitener {
        void onItemClick( int position,String rechargeMoneyId);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    /**end*/


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new OneViewHolder(LayoutInflater.from(context).inflate(R.layout.grideview_item_recharge, parent, false));

    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {

        holder.setData(dataList, position);
    }


    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        void setData(ArrayList<RechargeMoneyDetails> data, int position) {
        }
    }

    private class OneViewHolder extends BaseViewHolder {
        private TextView tvMoneyCount;
        private TextView tvFishStampsCount;
        private LinearLayout layPress;


        public OneViewHolder(View view) {
            super(view);
            tvMoneyCount = (TextView) view.findViewById(R.id.money_count);
            tvFishStampsCount = (TextView) view.findViewById(R.id.fishStamps_count);
            layPress = (LinearLayout) view.findViewById(R.id.lay_press);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    getAdapterPosition();
                    if (lastPressIndex == position) {
                        lastPressIndex = -1;
                    } else {
                        lastPressIndex = position;
                    }
                    notifyDataSetChanged();


                }

            });
        }


        @Override
        void setData(ArrayList<RechargeMoneyDetails> data, int position) {
            if (data != null) {
               RechargeMoneyDetails details = data.get(position);
                tvMoneyCount.setText(details.money+"元");
                tvFishStampsCount.setText(details.ticket+"点券");
                if (getAdapterPosition() == lastPressIndex) {
                    layPress.setSelected(true);
                    layPress.setBackgroundResource(R.drawable.bg_recharge_item_press);
                    tvMoneyCount.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.white));
                    tvFishStampsCount.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.white));
                    mOnItemClickLitener.onItemClick(getAdapterPosition(),data.get(position).payId);
                } else {
                    layPress.setSelected(false);
                    layPress.setBackgroundResource(R.drawable.bg_recharge_item_normal);
                    tvMoneyCount.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.blue_500));
                    tvFishStampsCount.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.blue_500));
                }
            }

        }


    }
}


